<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of VideoController
 *
 * @author Administrator
 */
class VideoController extends Controller {

    public $start;
    public $end;

    public function actionIndex() {
        $search = new VideoSearch();
        $request = Yii::app()->request;
        if ($request->getIsPostRequest()) {
            $search->attributes = $request->getPost('VideoSearch');
        }
        $this->render('index', array('model' => $search));
    }

    public function actionCreate() {
        $model = new Video();
        $this->performAjaxValidation($model);
        $request = Yii::app()->request;
        if ($request->getIsPostRequest()) {
            $form = $request->getPost('Video');
            $model->attributes = $form;
            if (!$form['video_id']) { //保持基本信息
                if ($model->save()) { //保存成功
                    Yii::app()->cookies->category = CJSON::encode($model->category);
                    $this->redirect($this->createUrl('next', array('id' => $model->getPrimaryKey())));
                    exit;
                }
            }
        } else {
            $model->video_region = Site::model()->getConfig('site_video_region');
            $model->video_quality = Site::model()->getConfig('site_video_quality');
            $model->video_dialog = Site::model()->getConfig('site_video_dialog');
            $model->video_allow_play = Site::model()->getConfig('site_video_play');
            $model->video_allow_down = Site::model()->getConfig('site_video_down');
            $model->video_play_money = (int) Site::model()->getConfig('site_video_money');
            $model->video_down_money = (int) Site::model()->getConfig('site_video_money');
            $model->video_player_alias = Site::model()->getConfig('site_video_player');
            $model->video_poster = Site::model()->getConfig('site_video_pic');
            $model->video_thumb = Site::model()->getConfig('site_video_pic');
            $video_category = Yii::app()->cookies->category;

            if ($video_category) {
                $model->category = CJSON::decode($video_category);
            }
        }
        $this->render('create', array('model' => $model));
    }

    public function actionNext() {
        $model = new VideoFiles('make');
        $request = Yii::app()->request;
        $vid = (int) Yii::app()->request->getParam('id');
        $model->video_id = $vid;
        $errors = array();
        if ($request->getIsPostRequest()) {
            $form = $request->getPost('VideoFiles');
            $this->start = $form['start'];
            $this->end = $form['end'];
            $model->attributes = $form;

            if ($request->getPost('make')) {
                CActiveForm::validate($model);
            } else {
                $model->setScenario('insert');
                if (is_array($form['server'])) { //判断是有选择服务器就写入服务器数据
                    VideoServerId::model()->deleteAllByAttributes(array('video_id' => $vid));
                    foreach ($form['server'] as $sid) {
                        $server = new VideoServerId();
                        $server->video_id = $vid;
                        $server->server_id = $sid;
                        if (!$server->save()) {
                            foreach ($server->getErrors() as $attribute => $error) {
                                echo $errors[$attribute] = $error[0];
                            }
                        }
                    }
                    if (count($errors) <= 0) { //写入服务器数据没问题就继续
                        for ($i = 0; $i < count($form['url']); $i++) {
                            $file = new VideoFiles('insert');
                            $file->start = $form['start'];
                            $file->end = $form['end'];
                            $file->file_path = $form['url'][$i];
                            $file->file_name = $form['name'][$i];
                            $file->file_sort = $i + 1;
                            $file->file_play_mode = $form['type'][$i];
                            $file->video_id = $vid;
                            $file->fid = $form['fid'][$i];
                            $file->server = $form['server'];
                            $file->setIsNewRecord($form['fid'][$i] > 0 ? false : true);
                            if (!$file->save()) {
                                foreach ($file->getErrors() as $attribute => $error) {
                                    $errors[$attribute] = $error[0];
                                }
                            }
                        }
                    }
                }
                ////判断是否出错结束
                if (count($errors) > 0) {
                    foreach ($errors as $attribute => $error) {
                        $model->addError($attribute, $error);
                    }
                } else {
                    $message = '视频路径资料保存成功 <br /><a href="' . $this->createUrl('create') . '">在添加一个新视频</a>';

                    $this->render("/layouts/message", array('message' => $message, 'title' => '操作完成', 'url' => $request->getUrlReferrer()));
                    Yii::app()->end();
                }
            }//结束判断是否是生成路径
        } else {
            //影片的集数
            $num = sizeof($model->getFiles());
            $this->start = $num > 0 ? $num + 1 : 1;
            $this->end = $this->start + 1;
            $server = Yii::app()->cookies->server;
            if ($server) {
                $model->server = CJSON::decode($server);
            }
        }
        $this->render('next', array('model' => $model));
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'video-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function actionDelfile() {
        $request = Yii::app()->request;
        $fid = (int) $request->getParam('fid');
        $vid = (int) $request->getParam('vid');
        if ($fid) {
            $file = new VideoFiles();
            $criteria = new CDbCriteria;
            $criteria->addCondition("fid=:fid");
            $criteria->addCondition("video_id=:vid");
            $criteria->params[':fid'] = $fid;
            $criteria->params[':vid'] = $vid;
            $file->deleteAll($criteria);
            Yii::app()->end(0);
        } else {
            Yii::app()->end(1);
        }
    }

    /**
     * 修改视频
     */
    public function actionUpdate() {
        $video = new Video();
        $this->performAjaxValidation($video);
        $pk = (int) Yii::app()->request->getParam('id');
        $model = $video->findByPk($pk);
        if ($model) {
            $request = Yii::app()->request;
            if ($request->getIsPostRequest()) {
                $model->setScenario('insert');
                $model->attributes = $request->getPost('Video');
                if (!$model->save()) {
                    
                }
            } else {
                $model->video_region = $model->getRegion();
            }
            $this->render('update', array('model' => $model));
        } else {
            $this->render('/layouts/message', array('message' => Yii::t('default', '视频文件不存在')));
        }
    }

    public function actionDelete() {
        $request = Yii::app()->request;
        $vid = $request->getParam('id');
        if ($vid) {
            $video = new Video();
            if (is_array($vid)) {
                foreach ($vid as $pk) {
                    $video->deleteByPk((int) $pk);
                }
            } else {
                $video->deleteByPk((int) $vid);
            }
        }
        Yii::app()->end();
    }

}